clc;
clearvars;
close all;


FeaturesIndex={ 1:39%All Features Included
                [33,28,37,29,8,34,35,32,1,39,20,4,2,3,11,16,18,30,10];% Subset1 FrwSlc All
                [28,7,29,2,3,15]};% Subset3 FrwSlc FitExc
KNNMethodes={'KNN (\wp)',...
             'KNN (\wp_s_u_b_1)',...
             'KNN (\wp_s_u_b_2)'};
KNNMethodNum=length(KNNMethodes);


K=31;
            
[Train,Test]=GetNoisyDataFromRepository(true);

% [Train,Test]=GetKFoldRegressorDataFromRepository(10,true);
% F=1;
% Train.FittedW=Train.FittedW(:,:,F);
% Train.Label=Train.Label(:,F);
% Train.Features=Train.Features(:,:,F);
%%
Test.SetNum=1;

PredictedValue=zeros(Test.SNum,Test.SetNum,KNNMethodNum);
for k=1:KNNMethodNum
    KNNRegressor = KNNReg(Train.Features(:,FeaturesIndex{k}),Train.Label,'NumNeighbors',K,'RegresiorMethod','InverseDistance');
    for s=1:Test.SetNum
        display(['Analyze Feature for Set # ',num2str(s),' of ',num2str(Test.SetNum),' .',KNNMethodes{k}])
        PredictedValue(:,s,k)=GetRegValue(KNNRegressor,Test.Features(:,FeaturesIndex{k},s));
    end
end

%%
BestFittngMethodsNum=size(Test.BestFittedW,2);
BFMAE=zeros(BestFittngMethodsNum,Test.SetNum);
for f=1:BestFittngMethodsNum
for i=1:Test.SetNum
    Erorrs=Test.BestFittedW(:,f,i)-Test.Label(:,i);
    Erorrs(isnan(Erorrs))=[];
    AE = abs(Erorrs);
    BFMAE(f,i)=mean(AE);
end
end
%%
EMAE=zeros(KNNMethodNum,Test.SetNum);
EDnM=zeros(KNNMethodNum,Test.SetNum);
EUpM=zeros(KNNMethodNum,Test.SetNum);
for k=1:KNNMethodNum
for r=1:Test.SetNum
    Erorrs=PredictedValue(:,r,k)-Test.Label(:,r);
    AE = abs(Erorrs);
    EMAE(k,r)=mean(AE);
    EDnM(k,r)=mean(AE(AE<=EMAE(k,r)));
    EUpM(k,r)=mean(AE(AE>=EMAE(k,r)));
end
end
%%
disp(BFMAE)
disp(EMAE)